package com.net.http
{
	import com.net.HttpServer;
	
	import flash.utils.Dictionary;

	public class RequestHeader
	{
		private var requestParmeter:Dictionary;//其余的请求参数
		
		private var urlRequestParmeter:Dictionary;//网址附带的参数
		
		private var requestMode:String;//响应模式
		
		private var filePath:String;//请求的资源地址
		
		private var fileExtension:String;//请求的资源扩展名
		
		private var fileType:String;//请求的资源文件类型
		
		private var fileName:String;//请求的资源名字
		
		
		//-----------------------------------------------------------------------
		private const httpVersion:String="HTTP/1.1";//http协议版本
		
		private const ServerVersion:String="DTWebServer/1.0";//服务器版本
		
		private var reponseState:String;//响应的状态码
		
		private var reponseLength:String;//响应的内容大小
		
		private var reponseParmeter:String;//响应的额外参数
		
		
		/**
		 * 传入一个行参数初始化此请求头
		 */
		public function RequestHeader(oneLine:String)
		{
			reponseParmeter="";
			
			requestParmeter=new Dictionary();
			urlRequestParmeter=new Dictionary();
			//取得http方式
			requestMode=oneLine.substring(0,oneLine.indexOf(" "));
			
			//取得请求的地址
			var tempHtml:String = oneLine.substring(oneLine.indexOf(" ")+1, oneLine.indexOf("HTTP") - 1);
			if(tempHtml.charAt(0)=="/"){
				tempHtml=tempHtml.substring(1,tempHtml.length);
			}
			if(tempHtml.indexOf("?") != -1){
				filePath=tempHtml.substring(0, tempHtml.indexOf("?"));
				//处理链接传来的参数
				var parmas:Array = tempHtml.substring(filePath.length + 1, tempHtml.length).split("&");
				for each(var s:String in parmas){
					var li:int=s.indexOf("=");
					urlRequestParmeter[s.substring(0,li)]=s.substring(li+1,s.length);
				}
			}else{
				filePath=tempHtml;
			}
			
			//取得文件扩展名和类型 
			if(filePath.indexOf(".") != -1){
				//循环取得文件扩展名（得到最后一个“.”之后的字符串）
				fileExtension = filePath.substring(filePath.indexOf(".") + 1, filePath.length).toLowerCase();
				while(fileExtension.indexOf(".")>-1){
					fileExtension=fileExtension.substring(fileExtension.indexOf(".")+1,fileExtension.length);
				}
				if(fileExtension=="html" || fileExtension=="htm" || fileExtension=="txt" || fileExtension=="xml"){
					fileType="text/";
				}else if(fileExtension=="png" || fileExtension=="jpg" || fileExtension=="jpeg" || fileExtension=="bmp" || fileExtension=="gif" || fileExtension=="ico"){
					fileType="image/";
				}else if(fileExtension=="css"){
					fileType="application/octet-streamcss";
				}else if(fileExtension=="swf"){
					fileType="application/x-shockwave-flash";
				}else{
					fileType="application/octet-stream";
				}
			}else if(filePath==""){
				filePath=HttpServer.getDefaultPage();
				fileExtension=filePath.substring(filePath.indexOf(".")+1,filePath.length);
				fileType="text/";
			}else{
				fileExtension="";
				fileType="application/octet-stream";
			}
			//取得文件名
			fileName=filePath.substring(filePath.indexOf("/")+1,filePath.length);
			while(fileName.indexOf("/")>-1){
				fileName=fileName.substring(fileName.indexOf("/")+1,fileName.length);
			}
			
		}
		
		/**
		 * 写入一个参数
		 */
		public function pushParmeter(string:String):void
		{
			var inNum:int=string.indexOf(": ");
			if(inNum!=-1){
				requestParmeter[string.substring(0,inNum)]=string.substring(inNum+2,string.length);
			}else{
				inNum=string.indexOf(" ");
				requestParmeter[string.substring(0,inNum)]=string.substring(inNum+1,string.length);
			}
		}
		
		/**
		 * 设置响应需要的参数
		 * state reponse状态码
		 * type  数据类型
		 * extension 文件扩展名
		 * length  数据长度
		 * 
		 * 如果参数没有传入  则使用的是默认的数据
		 */
		public function setReponseHeader(state:String=null,type:String=null,extension:String=null,length:uint=0):void{
			if(state!=null){
				reponseState=state;
			}
			if(type!=null){
				fileType=type;
			}
			if(extension!=null){
				fileExtension=extension;
			}
			reponseLength=length.toString();
		}
		
		/**
		 * 加入一条http响应参数
		 */
		public function addReponseParmeter(name:String,content:String):void{
			reponseParmeter+=name+": "+content+"\r\n";
		}
		
		
		/**
		 * 生成响应报文
		 */
		public function getReponseHeader():String{
			var returnStr:String;
			//如果不能识别则让客户下载
			if(fileType=="image/"){
				returnStr= httpVersion + " " + reponseState+ "\r\n"
					+"Date: " + new Date().toString()+"\r\n"
					+"Server: "+ServerVersion + "\r\n"
					+"Connection: Keep-Alive" + "\r\n"
					+"Content-Type: " + fileType+fileExtension + "\r\n"
					+"Content-Length: " + reponseLength  + "\r\n"
					+reponseParmeter
					+"\r\n";
				return returnStr;
			}
			if(fileType=="text/"){
				returnStr= httpVersion + " " + reponseState+ "\r\n"
					+"Date: " + new Date().toString()+"\r\n"
					+"Server: "+ServerVersion + "\r\n"
					+"Connection: Keep-Alive" + "\r\n"
					+"Content-Type: " + fileType+fileExtension+";charset=utf-8"+ "\r\n"
					+"Content-Length: " + reponseLength  + "\r\n"
					+reponseParmeter
					+"\r\n";
				return returnStr;
			}
			if(fileType=="application/x-shockwave-flash"){
				returnStr= httpVersion + " " + reponseState+ "\r\n"
					+"Date: " + new Date().toString()+"\r\n"
					+"Server: "+ServerVersion + "\r\n"
					+"Connection: Keep-Alive" + "\r\n"
					+"Content-Type: " + fileType+ "\r\n"
					+"Content-Length: " + reponseLength  + "\r\n"
					+reponseParmeter
					+"\r\n";
				return returnStr;
			}
			returnStr=httpVersion + " " + reponseState + "\r\n"
				+"Date: " + new Date().toString() + "\r\n"
				+"Server: "+ServerVersion + "\r\n"
				+"Connection: Keep-Alive" + "\r\n"
				+"Content-Type: " + fileType+ "\r\n"//+fileExtension
				+"Content-Length: " + reponseLength  + "\r\n"
				+"Content-disposition: attachment;filename="+fileName+"\r\n"
				+reponseParmeter
				+"\r\n";
			return returnStr;
		}
		
		
		/**
		 * 请求模式
		 */
		public function getRequestMode():String{
			return requestMode;
		}
		
		/**
		 * 请求资源的url
		 */
		public function getFilePath():String{
			return HttpServer.getVD()+filePath;
		}
		
		/**
		 * 请求文件类型
		 */
		public function getFileType():String{
			return fileType;
		}
		
		/**
		 * 请求资源的扩展名
		 */
		public function getFileExtension():String{
			return fileExtension;
		}
		
		/**
		 * 取出http请求附带的参数
		 */
		public function getParmeter(s:String):String{
			if(requestParmeter[s]!=null){
				return requestParmeter[s] as String;
			}
			return "";
		}
		/**
		 * 取出请求的网址附带的参数
		 */
		public function getHttpParmeter(s:String):String{
			if(urlRequestParmeter[s]!=null){
				return urlRequestParmeter[s] as String;
			}
			return "";
		}
	}
}